package com.erp.erp_ui.warehouse.controller;

import com.erp.erp_ui.base.Base;
import com.erp.erp_ui.warehouse.req.AddInventoryReq;
import com.erp.erp_ui.warehouse.req.InventoryDetailStockProductReq;
import com.erp.erp_ui.warehouse.req.InventoryListPageReq;
import com.erp.erp_ui.warehouse.service.InventoryService;
import com.erp.erp_ui.warehouse.vo.InventoryDetailAndUpdateVo;
import com.erp.erp_ui.warehouse.vo.InventoryDetailStockProductVo;
import com.erp.erp_ui.warehouse.vo.InventoryListPageVo;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName : InventoryController
 * @Description : 盘点控制层
 * @Author : lst
 * @Date: 2020-07-24 14:40
 */
@Controller
@RequestMapping(value = "inventory")
public class InventoryController extends Base {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(InventoryController.class);

    /**
     * 盘点业务逻辑接口
     */
    private final InventoryService inventoryService;

    @Autowired
    public InventoryController(InventoryService inventoryService) {
        this.inventoryService = inventoryService;
    }

    /**
     * 盘点首页-获取分页信息
     * @param req 查询条件实体
     * @return
     */
    @GetMapping("/getListByPage")
    @ResponseBody
    public R getListByPage(InventoryListPageReq req){
        try {
            LOGGER.info("盘点-获取分页信息:" + req.toString());
            PageInfo<InventoryListPageVo> pageInfo = inventoryService.getListByPage(req);
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            LOGGER.info("盘点-获取分页信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取库存信息
     * @param req
     * @return
     */
    @GetMapping("/getStockListByPage")
    @ResponseBody
    public R getStockList(InventoryDetailStockProductReq req){
        try {
            LOGGER.info("盘点-获取库存信息 req=" + req.toString());
            PageInfo<InventoryDetailStockProductVo> stockVos = inventoryService.getStockList(req);
            return R.build(0,"", stockVos.getTotal(), stockVos.getList());
        }catch (Exception e){
            LOGGER.info("盘点-获取库存信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 盘点新增
     * @param req 新增参数
     */
    @PostMapping("/add")
    @ResponseBody
    public R add(@Validated @RequestBody AddInventoryReq req){
        LOGGER.info("盘点-盘点新增 req=" + req.toString());
        try {
            return inventoryService.add(req, "", "");
        }catch (Exception e){
            LOGGER.info("盘点-盘点新增异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

    /**
     * 盘点-查看详情
     * @param id 盘点id
     * @return 详情对象
     */
    @PostMapping("/detail")
    @ResponseBody
    public R getInventoryDetailInfo(@RequestBody Integer id){
        try {
            LOGGER.info("盘点-查看详情 id=" + id);
            InventoryDetailAndUpdateVo vo = inventoryService.getInventoryDetailInfo(id, false);
            return R.build(vo);
        }catch (Exception e){
            LOGGER.info("盘点-查看详情异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 盘点-修改
     * @param id 盘点id
     * @return 详情对象
     */
    @PostMapping("/update")
    @ResponseBody
    public R getInventoryUpdateInfo(@RequestBody Integer id){
        try {
            LOGGER.info("盘点-修改 id=" + id);
            InventoryDetailAndUpdateVo vo = inventoryService.getInventoryDetailInfo(id, true);
            return R.build(vo);
        }catch (Exception e){
            LOGGER.info("盘点-修改异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 删除盘点信息
     * @param id 盘点id
     * @param status 状态(1.草稿箱 2.待批箱 3.已批箱 4.完成箱 5.垃圾箱)
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public R deleteInventoryInfo(@RequestBody Integer id, @RequestBody Integer status){
        try {
            LOGGER.info("仓库管理-盘点-删除盘点信息 id=" + id + ",status=" + status);
            inventoryService.delete(id, status);
            return R.build("");
        }catch (Exception e){
            LOGGER.info("仓库管理-盘点-删除盘点信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }


    /**
     * 盘点数据操作接口
     * @param id 盘点id
     * @param operation 操作类型
     * 1.提交审批 2.审批通过 3.审批退回 4.完成 5.还原
     * @return
     */
    @PostMapping("/operation")
    @ResponseBody
    public R operation(@RequestBody Integer id, @RequestBody Integer operation){
        try {
            LOGGER.info("仓库管理-盘点-数据操作接口 id=" + id + ",operation=" + operation);
            return inventoryService.operation(id, operation);
        }catch (Exception e){
            LOGGER.info("仓库管理-盘点-数据操作接口异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


}
